/*
 * 
 */
package client;


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.Naming;
import java.rmi.Remote;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import server.Notify;
import server.chatInterface;
import server.notifyImplement;

/**
 *
 * @author Hanh
 */
public class ChatClient extends javax.swing.JFrame {
    private final int port = 1098;
    private final String server = "localhost";
    
    private JFrame frame;
    private JTextArea myText;
    private static JTextArea otherText;
    private JScrollPane myTextScroll;
    private JScrollPane otherTextScroll;
    private String textString = "";
    private boolean firstMessage = true;
    private static String name;
    
    private static final int WID_SIZE = 400;
    private static final int HIE_SIZE = 150;
    
    Notify displayChat;
    chatInterface chatServer;
    
    public ChatClient(){
        java.awt.EventQueue.invokeLater(new Runnable(){

            @Override
            public void run() {
                initComponents();
            }
            
        });
        try {
           Remote remoteObject = Naming.lookup("rmi://"+server+":"+port+"/chatrmi");;
           System.setProperty("java.security.policy", "client.policy");
           if(remoteObject instanceof chatInterface){
               chatServer = (chatInterface)remoteObject;
               displayChat = new notifyImplement(otherText);
           }else {
               System.out.println("Server not a Chat Server.");
	       System.exit(0);
           }
            
        }catch(Exception e){
            System.out.println("RMI Lookup Exception");
            System.exit(0);
        }
        
        frame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                try{
                    chatServer.leave(displayChat);
                }catch(Exception ex){
                    otherText.append("Exit failed.");
                }
                System.exit(0);
            }
        });
    }
    
    private void initComponents(){
        frame = new JFrame("Chat Client");
        myText = new JTextArea();
        
        myTextScroll = new JScrollPane(myText);
        myTextScroll.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        myTextScroll.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        myTextScroll.setMaximumSize(
                new java.awt.Dimension(WID_SIZE, HIE_SIZE));
        myTextScroll.setMinimumSize(
                new java.awt.Dimension(WID_SIZE,HIE_SIZE));
        myTextScroll.setPreferredSize(
                new java.awt.Dimension(WID_SIZE, HIE_SIZE));
        
        myText.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyTyped(java.awt.event.KeyEvent evt){
                textTyped(evt);
            }
        });
        
        frame.getContentPane().add(myTextScroll, java.awt.BorderLayout.NORTH);
        
        otherText = new JTextArea();
        otherTextScroll = new JScrollPane(otherText);
        otherText.setBackground(new java.awt.Color(200,200,200));
        otherTextScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        otherTextScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        otherTextScroll.setMaximumSize(new java.awt.Dimension(WID_SIZE, HIE_SIZE));
        otherTextScroll.setMinimumSize(new java.awt.Dimension(WID_SIZE,HIE_SIZE));
        otherTextScroll.setPreferredSize(new java.awt.Dimension(WID_SIZE, HIE_SIZE));
        otherText.setEditable(false);
        
        frame.getContentPane().add(otherTextScroll,java.awt.BorderLayout.CENTER);
        
        frame.pack();
        frame.setVisible(true);
    }
    
    private void textTyped(java.awt.event.KeyEvent evt){
        char c = evt.getKeyChar();
        if(c == '\n'){
            try{
                if(firstMessage){
                    displayChat.setName(textString);
                    chatServer.join(displayChat);
                    firstMessage = false;
                }else {
                   chatServer.talk(displayChat,textString);
                }
            }catch(Exception ie){
                otherText.append("Failed to send message.");
            }
            textString = "";
        }else {
            textString = textString + c;
        }
    }
    
    public static void main(String args[]){
        new ChatClient();
    }
}
